package com.beikai.demo01.single;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created with IntelliJ IDEA.
 *
 * @author : hanbeikai
 * Date: 2021/5/4 1:32 下午
 * Description: 单例模式
 */
public class SingleModel {
}

class Single01{
    private static final Single01 SINGLE01 = new Single01();
    private Single01(){
    }
    public static Single01 getSingle01(){
        return SINGLE01;
    }
}

class Single11{
    private static Single11 single11 = null;
    static {
        single11 = new Single11();
    }
    private Single11(){
    }
    public static Single11 getSingle11(){
        return single11;
    }
}

class Single02{
    private static Single02 single01 = null;
    private Single02(){
    }
    public static Single02 getSingle02(){
        if (null == single01) {
            single01 = new Single02();
        }
        return single01;
    }
}

class Single03{
    private static Single03 single03 = null;
    private Single03(){
    }
    public static synchronized Single03 getSingle03(){
        if (null == single03) {
            single03 = new Single03();
        }
        return single03;
    }
}

class Single04{
    private static Single04 single04 = null;
    private Single04(){
    }
    public static Single04 getSingle04() {
        if (null == single04) {
            synchronized (Single04.class){
                single04 = new Single04();
            }
        }
        return single04;
    }
}

class Single05{
    private static volatile Single05 single05 = null;
    private Single05(){
    }
    public static Single05 getSingle05(){
        if (null == single05) {
            synchronized (Single05.class){
                if (null == single05){
                    single05 = new Single05();
                }
            }
        }
        return single05;
    }
}

class Single06{
    static class Single06Inner{
        private final static Single06 SINGLE_06 = new Single06();
    }
    public static Single06 getSingle06(){
        return Single06Inner.SINGLE_06;
    }
}

enum Single07{
    //
    SINGLE_07;
}

/**
 * 容器类单例
 */
class InstanceManager{
    private static final Map<String,Object> CONTENT = new ConcurrentHashMap<>();
    private InstanceManager(){}

    /**
     * 注册
     * @param key
     * @param instance
     */
    public static void registry(String key,Object instance){
        if (!CONTENT.containsKey(key)){
            CONTENT.put(key,instance);
        }
    }
    public static Object getInstance(String key){
        return CONTENT.get(key);
    }
}

class demo{
    public static void main(String[] args) {
        InstanceManager.registry("dog",new Dog());

        Dog dog = (Dog) InstanceManager.getInstance("dog");
        Dog dog2 = (Dog) InstanceManager.getInstance("dog");
        Dog dog3 = (Dog) InstanceManager.getInstance("dog");
        Dog dog4 = (Dog) InstanceManager.getInstance("dog");

        System.out.println(dog);
        System.out.println(dog2);
        System.out.println(dog3);
        System.out.println(dog4);


    }
}

class Dog{

    public Dog(){}

}








